<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see 
 * <http://www.gnu.org/licenses/>.
*/


// no direct access
defined('_JEXEC') or die('Restricted access');


jimport('joomla.application.component.model');


class MassMailerModelSubscribersEdit extends JModel
{
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}

	function setId($id)
	{
		$this->_id   = $id;
		$this->_row = null;
	}

	function getRow()
	{
		if (empty($this->_row) && $this->_id > 0)
		{
			$sql = 'select * from #__mm_subscribers'
				. "\n	where id = " . (int)$this->_id
				;
			$this->_db->setQuery($sql);
			$this->_row = $this->_db->loadObject();
		}
		else
		{
			$this->_row = new stdClass();
			$this->_row->id = 0;
			$this->_row->name = '';
			$this->_row->email = '';
			$this->_row->created = null;
		}

		return $this->_row;
	}

	function store()
	{
		$row = $this->getTable('Subscribers');
		$data = JRequest::get('post');

		if (!$row->bind($data))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->check())
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->store())
		{
			$this->setError($row->getErrorMsg());
			return false;
		}

		return true;
	}

	function delete()
	{
		$cids = JRequest::getVar('cid', array(0), 'post', 'array');
		$row = $this->getTable('Subscribers');

		foreach($cids as $cid)
		{
			if (!$row->delete($cid))
			{
				$this->setError($row->getErrorMsg());
				return false;
			}
		}
		return true;
	}

	private $_id = null;

	private $_row = null;
}


?>
